const {
    getBikeListServe,
    deleteBikeServe,
    addBikeServe,
    updateBikeServe,
    getBikeByIdServe,
    updateBikeStatusServe
} = require('../serve/bike.serve')
const {btnAuth} = require("../middleware/auth.middleware");

class BikeController {
    async getBikeList(ctx) {
        if (!(btnAuth(ctx, 'btn.Bike.add') || btnAuth(ctx, 'btn.Bike.update') || btnAuth(ctx, 'btn.Bike.remove'))) {
            return ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
        try {
            const res = await getBikeListServe(ctx)
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: {
                    total: res.total,
                    pageSize: res.pageSize,
                    pageNum: res.pageNum,
                    pageData: res.data
                }
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async addBike(ctx) {
        if (!btnAuth(ctx, 'btn.Bike.add')) {
            return ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
        try {
            await addBikeServe(ctx);
            ctx.body = {
                msg: '添加成功',
                status: 0
            }
        } catch (e) {
            console.log(e);
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async updateBike(ctx) {
        if (!btnAuth(ctx, 'btn.Bike.update')) {
            return ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
        const {id} = ctx.params;
        const res = await getBikeByIdServe(id);
        if (!res) {
            return ctx.body = {
                status: 1,
                msg: '未找到id对应数据'
            }
        }
        try {
            const isSuccessUpdate = await updateBikeServe(ctx, id)
            ctx.body = {
                msg: '修改成功',
                status: 0,
                data: isSuccessUpdate
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async deleteBike(ctx) {
        if (!btnAuth(ctx, 'btn.Bike.remove')) {
            return ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
        const {id} = ctx.params;
        if (!id) {
            return ctx.body = {
                msg: 'id不能为空'
            }
        }
        console.log(id);
        const res = await getBikeByIdServe(id);
        if (!res) {
            return ctx.body = {
                status: 1,
                msg: '未找到id对应数据'
            }
        }
        try {
            const isSuccessDelete = await deleteBikeServe(id);
            ctx.body = {
                msg: '删除成功',
                status: 0,
                data: isSuccessDelete
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async getBikeDetail(ctx) {
        const {id} = ctx.params;
        try {
            const res = await getBikeByIdServe(id)
            ctx.body = {
                data: res,
                status: 0,
                mag: '获取成功'
            }
        } catch (e) {
            ctx.body = {
                status: 0,
                mag: e.message
            }
        }
    }

    async updateBikeStatus(ctx) {
        const {id} = ctx.params;
        const {status} = ctx.request.body;
        console.log(id, status);
        try {
            const isSuccessUpdate = await updateBikeStatusServe(id, status)
            ctx.body = {
                msg: '修改成功',
                status: 0,
                data: isSuccessUpdate
            }
        } catch (e) {
            ctx.body = {
                msg: '修改失败',
                status: 1,
            }
        }
    }
}

module.exports = new BikeController();
